import swal from 'sweetalert';
export default {
  install(Vue,options){
    //为dom添加no_Click属性，判断是否要等待
    Vue.prototype.$noClick = function(dom,time = 2000){
      var b = true;
      if(dom.getAttribute('no_click') == 0){
        swal('请勿频繁点击');
      }else{
        setTimeout(function(){
          dom.setAttribute('no_click',1);
        },time);
        dom.setAttribute('no_click',0);
        b = false;
      }
      return b;
    }
    //接口，数据，成功后的提示，成功后的跳转路径
    Vue.prototype.$ajax = async function({Interface,data,title = '保存成功！',url}){
      var d = await Interface(data);
      if(d){
        setTimeout(()=>{
          if(url){
            this.$router.push(url);
            swal.close();
          }else{
            location.reload();
          }
        },600);
        swal(title,'','success');
      }else{
        swal('出错了!','','error');
      }
    }
    //pageAjax:{
    //           page:true,//是否还有页
    //           time:'',//定时器
    //           books: [],//加载的数据
    //         },
    //Interface接口
    Vue.prototype.$pageAjax = function(pageAjax,Interface) {
      if (!pageAjax.page) return
      clearTimeout(pageAjax.time);
      pageAjax.time = setTimeout(async function () {
        let dom = document.documentElement;
        if (dom.scrollHeight - dom.clientHeight - dom.scrollTop < 100) {
          pageAjax.page = false;
          let {data, start} = await Interface(pageAjax.books.length);
          pageAjax.books = [...pageAjax.books, ...data];
          pageAjax.page = start;
        }
      }, 30);
    }
  }
}


